﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using UIFrameWork;
using UnityEngine;

public class GameFramework : SingletonMono<GameFramework>
{
	public static int s_renderFPS = 30;
	public static float s_targetFrameTime = 1000f / (float) s_renderFPS;
	private  bool lockFPS_game = false;
	private float accumTime;//累加器
	private float accum;
	private float lastUpdateTime;
	private float frequency = 0.1f;
	private int frames;
	public static float m_FPS = 0f; 
	
	public bool LockFPS_Game
	{
		get { return lockFPS_game; }
		set
		{
			if (value == this.lockFPS_game)
			{
				return;
			}
			this.lockFPS_game = value;
			this.SetTargetFrameRate();
		}
	}

	public static int unityTargetFrameRate
	{
		get { return 60; }
	}

	public void SetTargetFrameRate()
	{
		if (this.lockFPS_game)
		{
			Application.targetFrameRate = GameFramework.unityTargetFrameRate;
		}
		else
		{
			Application.targetFrameRate = GameFramework.s_renderFPS;
		}
	}
	
	
	
	
	protected override void Init()
	{
		
	}

	protected override void UnInit()
	{
		
	}

	private void InitBaseSys()
	{
		Singleton<TimeManager>.CreateInstance();
		Singleton<GameObjectPool>.CreateInstance();
		Singleton<UIEventManger>.CreateInstance();
	}
	private void Start()
	{
		InitBaseSys();
	}

	private void PrepareSoundSystem(bool applySoundSetting = false)
	{
		
	}
	
	private void Update()
	{
		UpdateElse();
	}

	public void UpdateElse()
	{
		Singleton<TimeManager>.instance.Update();
		Singleton<GameObjectPool>.instance.Update();
		if (!this.LockFPS_Game)
		{
			
			float num = Time.realtimeSinceStartup - this.lastUpdateTime;
			this.accumTime += num;
			this.accum += 1f / num;
			this.lastUpdateTime = Time.realtimeSinceStartup;
			this.frames++;
			if (this.accumTime >= this.frequency)
			{
				GameFramework.m_FPS = this.accum / (float) this.frames;
//				GameFramework.m_FPS = (float) this.frames /this.accumTime ;
				Debug.LogError(GameFramework.m_FPS);
				this.accumTime = 0f;
				this.accum = 0f;
				this.frames = 0;
			}
		}
	}

	private void LateUpdate()
	{
		
	}

	private void OnDestroy()
	{
		
	}

	private void DestoryBaseSys()
	{
		
	}

	private void DestoryCoreSys()
	{
		
	}

	private void OnApplicationPause(bool pauseStatus)
	{
		
	}

	private void OnApplicationFocus(bool hasFocus)
	{
		
	}

	private void OnApplicationQuit()
	{
		
	}
}
